﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Threading;
using System.IO;

namespace SmallTalker
{
    /// <summary>
    /// Interaction logic for aero.xaml
    /// </summary>
    public partial class aero : Window
    {
        //this is all pretty much aero code...
        public aero()
        {
            InitializeComponent();
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };

        [DllImport("DwmApi.dll")]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
        public DispatcherTimer timer1 = new DispatcherTimer();
        System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon(); // Declaration
        System.Windows.Forms.ContextMenu contmen = new System.Windows.Forms.ContextMenu();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
                IntPtr myHwnd = windowInteropHelper.Handle;
                HwndSource mainWindowSrc = System.Windows.Interop.HwndSource.FromHwnd(myHwnd);

                mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

                MARGINS margins = new MARGINS()
                {
                    cxLeftWidth = -1,
                    cxRightWidth = -1,
                    cyBottomHeight = -1,
                    cyTopHeight = -1
                };
                DwmExtendFrameIntoClientArea(myHwnd, ref margins);
            }
            catch
            {
                MessageBox.Show("Ouch... this is embaressing; SAL has just failed! plz report 2 the team 'error aero', and if ur running XP juz say 'error airxp' thnx a lot!");
                this.Close();
            }
            //start of real code
            try
            {
                if (Environment.OSVersion.Version.Major < 6) //will handle non aero-compliant Operating systems (think the 50% markt share XP still has...)
                {
                    this.Background = Brushes.Honeydew; //change this if u want
                }
                timer1.Interval = new TimeSpan(0, 0, RandomNumber(10, 1000)); //between 10 and 1000 seconds norm, now its 10 and 10 so we can debug quick
                timer1.Tick += new EventHandler(timer1_tick);
                timer1.IsEnabled = true;
                MouseDown += delegate { DragMove(); };
            }
            catch
            {
                MessageBox.Show("error setup");
                this.Close();
            }
        }

        void aero_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?", "Really?", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                real = 1;
                this.Close();
            }
        }
        int real = 0;
        void aboutclick(object sender, EventArgs e)
        {
            var about = new About();
            about.Show();
        }
        private void timer1_tick(object sender, EventArgs e)
        {
            var asker = new asker();
            asker.Show();
            timer1.Interval = new TimeSpan(0,0,RandomNumber(10, 1000)); //again, normally between 10 and 1000 seconds, but now 10 and 10
        }
        private int RandomNumber(int min, int max)
        {
            try
            {
                Random random = new Random();
                return random.Next(min, max);
            }
            catch
            {
                MessageBox.Show("BA HUMBUG! I will not show u that! Were sorry, but SAL is acting a little selfish... any way, if you have extra time could you tell the team 'error: ran#'? Thnx a lot! See you later...");
                this.Close();
                return 0;
            }
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            var talker = new talker();
            talker.Show();
        }
        private void settingshow(object sender, EventArgs e)
        {
            var settings = new settings();
            settings.Show();
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (button3.Content.ToString() == "Pause")
            {
                timer1.IsEnabled = false;
                button3.Content = "Play";
            }
            else
            {
                timer1.IsEnabled = true;
                button3.Content = "Pause";
            }
        }
        private void showdtalk(object sender, EventArgs e)
        {
            var talker = new talker();
            talker.Show();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

    
        private void shower(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.WindowState = System.Windows.WindowState.Normal;
        }
        private void stopit(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (real == 0)
            {
                e.Cancel = true;
                this.WindowState = System.Windows.WindowState.Minimized;
            }
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            real = 1;
            this.Close();
        }
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            var asker = new asker();
            asker.Show();
        }
    }
}
